package com.example.famhealing.schedule;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.example.famhealing.R;
import com.example.famhealing.timeline.ConditionFragment;

public class CalendarPopup extends Activity{

	//월별 캘린더 뷰 객체
	CalendarMonthView monthView;
	//월별 캘린더 어댑터
	CalendarMonthAdapter monthViewAdapter;
			
	TextView monthText;
	
	int curYear;
	int curMonth;
	int curDay;
	
	public static String date= "";
	
	String month;
	String day;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.popup_calendar);
        
        //뒷배경 흐리게
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,
				WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
        
        // 월별 캘린더 뷰 객체 참조
        monthView = (CalendarMonthView) findViewById(R.id.monthView);
        monthViewAdapter = new CalendarMonthAdapter(this);
        monthView.setAdapter(monthViewAdapter);

        // 리스너 설정
        monthView.setOnDataSelectionListener(new OnDataSelectionListener() {
			public void onDataSelected(AdapterView parent, View v, int position, long id) {
				// 현재 선택한 일자 정보 표시
				MonthItem curItem = (MonthItem) monthViewAdapter.getItem(position);
				curDay = curItem.getDay();
				
				Toast.makeText(CalendarPopup.this, Integer.toString(curDay), Toast.LENGTH_SHORT).show();

				Log.d("CalendarMonthViewActivity", "Selected : " + curDay);
				
				if(curMonth + 1 < 10) {
					month = "0" + Integer.toString(curMonth+1);
				} else {
					month = Integer.toString(curMonth);
				}
				
				if(curDay < 10) {
					day = "0" + Integer.toString(curDay);
				} else {
					day = Integer.toString(curDay);
				}
				
				date += Integer.toString(curYear) + month + day + "/";
				
				ConditionFragment.searchDate = date;

			}
		});

        monthText = (TextView) findViewById(R.id.monthText);
        setMonthText();

        // 이전 월로 넘어가는 이벤트 처리
        ImageButton monthPrevious = (ImageButton) findViewById(R.id.monthPrevious);
        monthPrevious.setOnClickListener(new OnClickListener() {
        	public void onClick(View v) {
        		monthViewAdapter.setPreviousMonth();
        		monthViewAdapter.notifyDataSetChanged();

        		setMonthText();
        	}
        });

        // 다음 월로 넘어가는 이벤트 처리
        ImageButton monthNext = (ImageButton) findViewById(R.id.monthNext);
        monthNext.setOnClickListener(new OnClickListener() {
        	public void onClick(View v) {
        		monthViewAdapter.setNextMonth();
        		monthViewAdapter.notifyDataSetChanged();

        		setMonthText();
        	}
        });
	}
	
	//캘린더 상단 년,월 표시
	private void setMonthText() {
    	curYear = monthViewAdapter.getCurYear();
        curMonth = monthViewAdapter.getCurMonth();

        monthText.setText(curYear + "년" + (curMonth+1) + "월");
    }
	
	public boolean onKeyDown( int KeyCode, KeyEvent event ) {
	 
		if( event.getAction() == KeyEvent.ACTION_DOWN ){
			// 이 부분은 특정 키를 눌렀을때 실행 된다.
			// 만약 뒤로 버튼을 눌럿을때 할 행동을 지정하고 싶다면
	 
			if( KeyCode == KeyEvent.KEYCODE_BACK ){
				//백버튼 눌렸을 때
				if(CalendarMonthAdapter.multiSelect)
					date = date.substring(0, date.lastIndexOf("/"));

				finish();
	 
				return false;

			}
	 
		}
	 
		return super.onKeyDown( KeyCode, event );
	}
}